package cn.mdmm.controller.pageContent;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.pageContent.PageContentInfoModel;
import cn.mdmm.entity.response.ResponseBodyToGridData;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.service.pageContent.PageContentInfoService;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value="pageContent")
public class PageContentController extends BaseController {
	
	@Autowired
	TypeInfoService typeInfoService;
	@Autowired
    private SysDictService sysDictService;
	@Autowired
	private PageContentInfoService contentInfoService;
	
	/**
	 * 页面板块管理
	 * @param request
	 * @param newsType
	 * @return
	 */
	@RequestMapping(value = "/{typeEnglishName}/init")
	public String init(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName){
		Map<String, String> typeMap  = typeInfoService.getAllTypeToMap(typeEnglishName);
		Map<String, String> languageMap = sysDictService.getDetailValueDetailDescMap("语言");
		request.setAttribute("typeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		request.setAttribute("languageMap", JSONUtils.toJson(languageMap));
		request.setAttribute("languageCombo", JSONUtils.toCombo(languageMap));
		return "pageContent/"+typeEnglishName+"/list";
	}
	
	/**
	 * <p>获取页面板块列表</p>  
	 * @param request
	 * @param language
	 * @param title
	 * @param type
	 * @param level
	 * @return   
	 * 创建人：邓强   <br>
	 * 创建时间：2017年11月7日 下午4:43:32    <br>
	 */
	@RequestMapping(value = "/{typeEnglishName}/list")
	@ResponseBody
	@OpLog(logDesc="获取页面板块列表")
	public ResponseBodyToGridData findByConditions(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName, String language,String title, Integer type){
		ResponseBodyToGridData responseBodyToGridData = new ResponseBodyToGridData();
		type = typeInfoService.getTypeId(typeEnglishName, type);
		try{
			Page po = getPage(request, "`CREATE_DATE` DESC");
			po.addCondition("language", language);
			po.addCondition("title", title);
			po.addCondition("type", type);
			GridDataModel gridDataModel = contentInfoService.getGridDataModelByCondition(po);
			responseBodyToGridData.setData(gridDataModel);
		} catch(Exception e){
			logger.error("获取板块列表", e);
			responseBodyToGridData.addErrInfo("999", "获取页面板块列表异常", null);
		}
		return responseBodyToGridData;
	}
	
	@RequestMapping(value = "/{typeEnglishName}/toAdd")
	public String showAdd(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName) throws Exception{
		Map<String, String> languageMap = sysDictService.getDetailValueDetailDescMap("语言");
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeInfoService.getAllChildrenTypeToMap(typeEnglishName)));
		request.setAttribute("languageCombo", JSONUtils.toCombo(languageMap));
		Integer type = typeInfoService.getTypeId(typeEnglishName, null);
		request.setAttribute("type", type);
		return "pageContent/"+typeEnglishName+"/add";
	}
	
	/**
     * @Description: 
     * @param request
     * @param multipartFile
    */
	@RequestMapping(value = "/{typeEnglishName}/insert")
	@OpLog(logDesc="新增页面板块")
	public String insert(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName,PageContentInfoModel pageModel){
		Integer type = typeInfoService.getTypeId(typeEnglishName, pageModel.getType());
		pageModel.setType(type);
		Date curr = new Date();
		pageModel.setCreateDate(curr);
		try {
			contentInfoService.insert(pageModel);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}
	
	@RequestMapping(value = "/{typeEnglishName}/showEdit")
	public String showEdit(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName,Integer id){
		Map<String, String> languageMap = sysDictService.getDetailValueDetailDescMap("语言");
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeInfoService.getAllChildrenTypeToMap(typeEnglishName)));
		request.setAttribute("languageCombo", JSONUtils.toCombo(languageMap));
		PageContentInfoModel pageModel = contentInfoService.findById(id);
		request.setAttribute("pageModel", pageModel);
		return "pageContent/"+typeEnglishName+"/edit";
	}
	
	/**
     * @Description: 
     * @param request
     * @param multipartFile
     * @return
    */
	@RequestMapping(value = "/{typeEnglishName}/update")
	@OpLog(logDesc="修改板块信息")
	public String update(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName,PageContentInfoModel pageModel){
		Integer type = typeInfoService.getTypeId(typeEnglishName, pageModel.getType());
		pageModel.setType(type);
		Date curr = new Date();
		pageModel.setEditDate(curr);
		try {
			contentInfoService.mergeById(pageModel);
		} catch (SystemException e) {
			return "error";
		}
		return "success";
	}

	@RequestMapping(value = "/{typeEnglishName}/delete")
	@ResponseBody
	public ResponseBodyToOne delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("typeEnglishName")String typeEnglishName,Integer[] ids){
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			contentInfoService.removeByIds(ids);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}
}
